<template>
  <div class="favorites-toolbar">
    <!-- 左侧搜索和筛选 -->
    <div class="toolbar-left">
      <el-input
        v-model="searchKeyword"
        :placeholder="t('favorites.toolbar.searchPlaceholder')"
        :prefix-icon="Search"
        clearable
        style="width: 300px"
        @input="handleSearch"
      />
      


      <el-button-group>
        <el-button
          :type="viewMode === 'list' ? 'primary' : ''"
          :icon="List"
          @click="handleViewModeChange('list')"
        >
          {{ t('favorites.toolbar.listView') }}
        </el-button>
        <el-button
          :type="viewMode === 'grid' ? 'primary' : ''"
          :icon="Grid"
          @click="handleViewModeChange('grid')"
        >
          {{ t('favorites.toolbar.gridView') }}
        </el-button>
      </el-button-group>
    </div>

    <!-- 右侧操作按钮 -->
    <div class="toolbar-right">
      <el-button
        :icon="Refresh"
        :loading="loading"
        @click="handleRefresh"
      >
        {{ t('favorites.toolbar.refresh') }}
      </el-button>

      <el-button
        type="primary"
        :icon="Plus"
        @click="handleCreateNote"
      >
        {{ t('favorites.toolbar.newNote') }}
      </el-button>

      <el-dropdown @command="handleMoreAction">
        <el-button :icon="More">
          {{ t('favorites.toolbar.more') }}
          <el-icon class="el-icon--right"><ArrowDown /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item command="import">
              <el-icon><Upload /></el-icon>
              {{ t('favorites.toolbar.import') }}
            </el-dropdown-item>
            <el-dropdown-item command="export">
              <el-icon><Download /></el-icon>
              {{ t('favorites.toolbar.export') }}
            </el-dropdown-item>
            <el-dropdown-item divided command="batch-delete">
              <el-icon><Delete /></el-icon>
              {{ t('favorites.toolbar.batchDelete') }}
            </el-dropdown-item>
            <el-dropdown-item command="settings">
              <el-icon><Setting /></el-icon>
              {{ t('favorites.toolbar.settings') }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import {
  Search, List, Grid, Refresh, Plus, More, ArrowDown,
  Upload, Download, Delete, Setting
} from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

// Props
interface Props {
  viewMode: 'list' | 'grid'
  searchKeyword: string
  loading: boolean
}

const props = defineProps<Props>()

// 国际化
const { t } = useI18n()

// 路由
const router = useRouter()

// 权限检查
const authStore = useAuthStore()

// Emits
const emit = defineEmits<{
  'update:viewMode': [value: 'list' | 'grid']
  'update:searchKeyword': [value: string]
  'refresh': []
  'create-note': []
}>()

// 计算属性
const viewMode = computed({
  get: () => props.viewMode,
  set: (value) => emit('update:viewMode', value)
})

const searchKeyword = computed({
  get: () => props.searchKeyword,
  set: (value) => emit('update:searchKeyword', value)
})

// 防抖搜索
let searchTimer: NodeJS.Timeout | null = null

const handleSearch = () => {
  if (searchTimer) {
    clearTimeout(searchTimer)
  }
  searchTimer = setTimeout(() => {
    // 搜索逻辑已通过 v-model 自动触发
  }, 300)
}

const handleViewModeChange = (mode: 'list' | 'grid') => {
  viewMode.value = mode
}

const handleRefresh = () => {
  emit('refresh')
}

const handleCreateNote = () => {
  emit('create-note')
}

const handleMoreAction = (command: string) => {
  switch (command) {
    case 'import':
      handleImport()
      break
    case 'export':
      handleExport()
      break
    case 'batch-delete':
      handleBatchDelete()
      break
    case 'settings':
      handleSettings()
      break
  }
}

const handleImport = () => {
  ElMessage.info(t('favorites.messages.importInDevelopment'))
}

const handleExport = () => {
  ElMessage.info(t('favorites.messages.exportInDevelopment'))
}

const handleBatchDelete = () => {
  ElMessage.info(t('favorites.messages.batchDeleteInDevelopment'))
}

const handleSettings = () => {
  ElMessage.info(t('favorites.messages.settingsInDevelopment'))
}
</script>

<style scoped>
.favorites-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.el-button-group {
  margin-left: 8px;
}

.el-dropdown {
  margin-left: 8px;
}

@media (max-width: 768px) {
  .favorites-toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }
  
  .toolbar-left {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  
  .toolbar-left .el-input {
    width: 100% !important;
  }
  
  .toolbar-left .el-select {
    width: 100% !important;
  }
  
  .toolbar-right {
    justify-content: center;
  }
}
</style>
